package com.crazy.crazybusinessservice.controller;


import com.crazy.crazybusinessservice.VO.CardQuery;
import com.crazy.crazybusinessservice.VO.ShareQuery;
import com.crazy.crazybusinessservice.VO.TransactionDTO;
import com.crazy.crazybusinessservice.VO.TransactionQuery;
import com.crazy.crazybusinessservice.entity.BankcardRecord;
import com.crazy.crazybusinessservice.entity.ShareRecord;
import com.crazy.crazybusinessservice.service.BankCardRecordService;
import com.crazy.crazybusinessservice.service.ShareRecordService;
import com.crazy.crazybusinessservice.service.TransactionService;
import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazycommon.common.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Transaction")
public class TransactionController {

    @Autowired
    private TransactionService transactionService;
    @Autowired
    private BankCardRecordService bankCardRecordService;
    @Autowired
    private ShareRecordService shareRecordService;

    //模糊分页查询已确认的订单
    @PostMapping("/pageTransaction/{current}/{limit}")
    public BaseResponse<List> searchTransactions(
            @PathVariable Long current,
            @PathVariable Long limit,
            @RequestBody(required = true) TransactionQuery transactionQuery) {

        //获取客户相关信息
        String customerName=transactionQuery.getCustomerName();
        int customerType=transactionQuery.getCustomerType();
        String customerID=transactionQuery.getCustomerID();

        //获取产品信息
        int productId=transactionQuery.getProductId();
        String productName=transactionQuery.getProductName();

        //获取交易信息
        int status=1;//已确认的订单
//        Object statusObj = transactionQuery.getStatus(); // 返回的是Integer对象
//        if(statusObj!=null) {
//            status=transactionQuery.getStatus();
//        }
        int transactionType=transactionQuery.getTransactionType();

        //获取开始、结束时间
        Date begin=transactionQuery.getBegin();
        Date end=transactionQuery.getEnd();

        limit= Long.valueOf(10);
        current = (current-1)*limit;


        List<TransactionDTO> list=transactionService.findTransactionsByFuzzyCriteria(
                customerName,
                customerType,
                customerID,
                productId,
                productName,
                status,
                transactionType,
                begin,
                end,
                current,
                limit
        );

        return ResultUtils.success(list);
    }


    //查询用户银行卡流水
    @PostMapping("/getcard")
    public BaseResponse<List> searchBankcardRecord(
            @RequestBody(required = true)CardQuery cardQuery) {

        String bankcard=cardQuery.getBankcard();

        //获取开始、结束时间
        Date begin=cardQuery.getBegin();
        Date end=cardQuery.getEnd();


        List<BankcardRecord> list=bankCardRecordService.findBankCardRecord(
                bankcard,
                begin,
                end
        );

        return ResultUtils.success(list);
    }

    //查询客户对应银行卡持仓份额的流水
    @PostMapping("/getShare")
    public BaseResponse<List> searchFundRecord(
            @RequestBody(required = true)ShareQuery shareQuery) {

        String bankcard=shareQuery.getBankcard();
        Integer productID=shareQuery.getProductId();

        //获取开始、结束时间
        Date begin=shareQuery.getBegin();
        Date end=shareQuery.getEnd();


        List<ShareRecord> list=shareRecordService.findShareRecord(
                bankcard,
                productID,
                begin,
                end
        );

        return ResultUtils.success(list);
    }

}
